<template>
  <div id="app">
    <TodoHeader @add="addTodo"></TodoHeader>
    <TodoMain @del="delTodo" :todolist="list"></TodoMain>
    <TodoFooter
      v-if="list.length > 0"
      @clear="clearTodo"
      :todolistlength="list.length"
    ></TodoFooter>
  </div>
</template>

<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";

const defaultTodoList = [
  { id: 1, name: "吃饭", isDone: false },
  { id: 2, name: "睡觉", isDone: false },
  { id: 3, name: "打豆豆", isDone: false },
];
export default {
  name: "App",
  data() {
    return {
      list: JSON.parse(localStorage.getItem("TodoList")) || defaultTodoList,
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    addTodo(v) {
      this.list.unshift({ id: +new Date(), name: v });
    },
    delTodo(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    clearTodo() {
      this.list = [];
    },
  },
  watch: {
    list: {
      deep: true,
      handler(val) {
        localStorage.setItem("TodoList", JSON.stringify(val));
      },
    },
  },
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
